home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
models
/
YoutubeModel.as
< prev
Wrap
Text File
|
2008-11-04
|
7KB
|
222 lines
package com.jeroenwijering.models
{
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.player.Model;
import flash.display.Loader;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import flash.net.URLRequest;
public class YoutubeModel implements ModelInterface
{
private var connected:Boolean;
private var loader:Loader;
private var metasent:Boolean;
private var model:Model;
private var outgoing:LocalConnection;
private var inbound:LocalConnection;
private var loading:Boolean;
public function YoutubeModel(param1:Model)
{
var url:* = undefined;
var ytb:* = undefined;
var mod:Model = param1;
super();
model = mod;
outgoing = new LocalConnection();
outgoing.allowDomain("*");
outgoing.allowInsecureDomain("*");
outgoing.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange);
inbound = new LocalConnection();
inbound.allowDomain("*");
inbound.allowInsecureDomain("*");
inbound.addEventListener(StatusEvent.STATUS,onLocalConnectionStatusChange);
inbound.client = this;
try
{
inbound.connect("_AS2_to_AS3");
connected = true;
}
catch(err:Error)
{
stop();
model.sendEvent(ModelEvent.ERROR,{"message":"Cannot connect to Youtube. Only one YouTube connection per computer can be made!"});
}
loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
url = model.skin.loaderInfo.url;
ytb = url.substr(0,url.lastIndexOf("/") + 1) + "yt.swf";
loader.load(new URLRequest(ytb));
}
public function onSwfLoadComplete() : void
{
outgoing.send("_AS3_to_AS2","setSize",320,240);
if(model.config["mute"] == true)
{
volume(0);
}
else
{
volume(model.config["volume"]);
}
if(loading)
{
load();
}
}
public function onLocalConnectionStatusChange(param1:StatusEvent) : void
{
}
public function onLoadChange(param1:Number, param2:Number, param3:Number) : void
{
model.sendEvent(ModelEvent.LOADED,{
"loaded":param1,
"total":param2,
"offset":param3
});
}
public function seek(param1:Number) : void
{
outgoing.send("_AS3_to_AS2","seekTo",param1);
play();
}
public function onStateChange(param1:Number) : void
{
switch(Number(param1))
{
case -1:
break;
case 0:
if(model.config["state"] != ModelStates.BUFFERING && model.config["state"] != ModelStates.IDLE)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
}
break;
case 1:
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
break;
case 2:
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
break;
case 3:
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
model.sendEvent(ModelEvent.BUFFER,{"percentage":0});
}
}
public function onTimeChange(param1:Number, param2:Number) : void
{
model.sendEvent(ModelEvent.TIME,{
"position":param1,
"duration":param2
});
if(!metasent)
{
model.sendEvent(ModelEvent.META,{
"width":320,
"height":240,
"duration":param2
});
metasent = true;
}
}
public function volume(param1:Number) : void
{
outgoing.send("_AS3_to_AS2","setVolume",param1);
}
private function errorHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
}
public function stop() : void
{
outgoing.send("_AS3_to_AS2","stopVideo");
}
public function load() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
if(connected)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
loading = true;
if(outgoing)
{
_loc1_ = getID(model.playlist[model.config["item"]]["file"]);
_loc2_ = model.playlist[model.config["item"]]["start"];
outgoing.send("_AS3_to_AS2","loadVideoById",_loc1_,_loc2_);
model.mediaHandler(loader);
}
}
}
public function onError(param1:String) : void
{
var _loc2_:* = undefined;
_loc2_ = model.playlist[model.config["item"]]["file"];
model.sendEvent(ModelEvent.ERROR,{"message":"YouTube error (video not found?):\n" + _loc2_});
stop();
}
private function getID(param1:String) : String
{
var _loc2_:* = undefined;
var _loc3_:* = undefined;
var _loc4_:* = undefined;
_loc2_ = param1.split("?");
_loc3_ = "";
for(_loc4_ in _loc2_)
{
if(_loc2_[_loc4_].substr(0,2) == "v=")
{
_loc3_ = _loc2_[_loc4_].substr(2);
}
}
if(_loc3_ == "")
{
_loc3_ = param1.substr(param1.indexOf("/v/") + 3);
}
if(_loc3_.indexOf("&") > -1)
{
_loc3_ = _loc3_.substr(0,_loc3_.indexOf("&"));
}
return _loc3_;
}
public function play() : void
{
outgoing.send("_AS3_to_AS2","playVideo");
}
public function pause() : void
{
outgoing.send("_AS3_to_AS2","pauseVideo");
}
public function quality(param1:Boolean) : void
{
}
}
}